package com.kingtop.system;

import com.kingtop.base.model.*;
import com.kingtop.base.service.*;
import org.kingtop.cache.app.AuthorityCacheUtil;
import org.kingtop.cache.app.RoleCacheUtil;
import org.kingtop.sys.SystemStartupListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 陈金波
 * @date 2018/2/28 22:20
 */
public class ApplicationStartupListener extends SystemStartupListener {

    @Autowired
    private IBaseRoleService baseRoleService;

    @Autowired
    private IBaseAuthorityService baseAuthorityService;

    @Autowired
    private IBaseRoleAuthorityService baseRoleAuthorityService;

    @Autowired
    private IBaseUserRoleService baseUserRoleService;

    @Autowired
    private IBaseUserAuthorityService baseUserAuthorityService;

    @Override
    protected void contextInitialized() {
        super.contextInitialized();
        List<BaseAuthority> authorities = this.baseAuthorityService.listAllAuthority();
        for (BaseAuthority authority : authorities) {
            AuthorityCacheUtil.set(authority);
        }
        List<BaseRole> roles = this.baseRoleService.listAll();
        for (BaseRole role : roles) {
            RoleCacheUtil.set(role);
        }

        List<BaseRoleAuthority> baseRoleAuthorities = this.baseRoleAuthorityService.listAll();
        AuthorityCacheUtil.setRoleAuthority(baseRoleAuthorities);

        List<BaseUserRole> baseUserRoles = this.baseUserRoleService.listAll();
        RoleCacheUtil.setUserRole(baseUserRoles);

        List<BaseUserAuthority> baseUserAuthorities = this.baseUserAuthorityService.listAll();
        AuthorityCacheUtil.setUserAuthority(baseUserAuthorities);

    }
}
